package org.i_plus.model.dao;



import org.i_plus.model.ComplainBean;
import org.i_plus.model.ComplainDAO;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.annotations.reflection.XMLContext.Default;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ComplainDAOHibernate implements ComplainDAO{
	
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public void selectC(int num){
		Session session = sessionFactory.getCurrentSession();
		ComplainBean bean = (ComplainBean)session.get(ComplainBean.class, num);
		System.out.println(bean.getC_fromacc());
		System.out.println(bean.getCom_num());
		System.out.println(bean.getKind());
		System.out.println(bean.getC_state());
		System.out.println(bean.getC_time());
		//session.close();	
	}
	@Override
	public void insertC(String c_fromacc,String com_num,String kind,String c_state,java.sql.Timestamp c_time){
		Session session = sessionFactory.getCurrentSession();
//		Transaction tx = session.beginTransaction();
		ComplainBean bean = new ComplainBean();
		bean.setC_fromacc(c_fromacc);
		bean.setCom_num(com_num);
		bean.setKind(kind);
		bean.setC_state(c_state);
		bean.setC_time(c_time);
		session.save(bean);
//		tx.commit();
		//session.close();
	}
	@Override
	public void updateC(String c_fromacc,String com_num,String kind,int num,String c_state,java.sql.Timestamp c_time){
		Session session = sessionFactory.getCurrentSession();
//		Transaction tx = session.beginTransaction();
		ComplainBean bean = (ComplainBean)session.get(ComplainBean.class, num);
		bean.setC_fromacc(c_fromacc);
		bean.setCom_num(com_num);
		bean.setKind(kind);
		bean.setC_state(c_state);
		bean.setC_time(c_time);
		session.saveOrUpdate(bean);
//		tx.commit();
		//session.close();
		
	}
	@Override
	public void deleteC(int num){
		Session session = sessionFactory.getCurrentSession();
//		Transaction tx= session.beginTransaction();
		ComplainBean bean = (ComplainBean) session.get(ComplainBean.class, num);
		session.delete(bean);
//		tx.commit();
		//session.close();
		
	}
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		
		SessionFactory session=(SessionFactory) context.getBean("sessionFactory");
		
		Transaction tx = session.getCurrentSession().beginTransaction();
		ComplainDAOHibernate cdao= (ComplainDAOHibernate)context.getBean("complainDAO");
		//cdao.insertC("123", "123", "F", "F", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0));
//		cdao.selectC(1);
//		cdao.updateC("456", "456", "F", 4, "T", new java.sql.Timestamp(1, 1, 0, 0, 0, 0, 0));
		cdao.deleteC(4);
		tx.commit();
		
//		ComplainDAOHibernate cdao = new ComplainDAOHibernate();
//		cdao.insertC("123", "123", "F", "F", new java.sql.Timestamp(0, 0, 0, 0, 0, 0, 0));
//		cdao.selectC(1);
//		cdao.updateC("456", "456", "F", 3, "T", new java.sql.Timestamp(1, 1, 0, 0, 0, 0, 0));
//		cdao.deleteC(2);
	}
}
